home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
listings
/
v_10_09
/
1009044a
< prev
next >
Wrap
Text File
|
1992-04-02
|
3KB
|
130 lines
/*** LISTING 2 ***/
/*** ***/
/*** set.c ***/
/*** **************************************** ***/
/*** SET SERIAL PARAMETERS ***/
/*** **************************************** ***/
#include "serial.h"
int portbase;
int SetSerial (int ComPort,long Baud,int Parity,
int DataBits,int StopBits)
{
if (SetPort(ComPort))
{
return (-1);
}
if (SetBaud(Baud))
{
return (-1);
}
if (SetOthers(Parity,DataBits,StopBits))
{
return (-1);
}
return (0);
}
/*** **************************************** ***/
/*** SET THE PORT VALUE TO CORRECT COM PORT ***/
/*** **************************************** ***/
int SetPort (int ComPort)
{
switch (ComPort)
{
case COM1: portbase = COM1BASE;
break;
case COM2: portbase = COM2BASE;
break;
case COM3: portbase = COM3BASE;
break;
case COM4: portbase = COM4BASE;
break;
default: return (-1);
}
return (0);
}
/*** **************************************** ***/
/*** SET THE BAUD RATE - DLAB MUST BE SET ON.***/
/*** **************************************** ***/
int SetBaud (long Baud)
{
char Current_Value;
int divisor;
if ((Baud <= 0) || (Baud > 115200L))
{
return (-1);
}
else
{
divisor = (int)(115200L/Baud);
}
/*** READ CURRENT VALUES IN LINE CONTROL REG ***/
Current_Value = inp(portbase + LCR);
/*** OR CURRENT VALUE WITH 0x80 TO SET DLAB ***/
outp(portbase + LCR,(Current_Value | DLAB));
/*** OUTPUT BAUD DIVISOR TO LOW BYTE ***/
outp(portbase + DLL,(divisor & 0x00FF));
/*** OUTPUT BAUD DIVISOR TO HIGH BYTE ***/
outp(portbase + DLH,((divisor >> 8) & 0x00FF));
/*** TURN DLAB OFF BY SENDING CURRENT VALUE ***/
outp(portbase + LCR,Current_Value);
return (0);
}
/*** **************************************** ***/
/*** SET PARITY, DATA BITS, AND STOPBITS ***/
/*** **************************************** ***/
int SetOthers (int Parity,int DataBits,int StopBits)
{
int setting;
if ((DataBits < 5) || (DataBits > 8))
{
return (-1);
}
if ((StopBits != 1 && StopBits != 2))
{
return (-1);
}
if ((Parity != NO_PARITY) && (Parity != ODD_PARITY)
&& (Parity != EVEN_PARITY))
{
return (-1);
}
setting = DataBits - 5;
setting |= ((StopBits == 1) ? 0x00 : 0x04);
setting |= Parity;
outp(portbase + LCR,setting);
return (0);
}